/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.ElevatorUpdateHistory;
import org.springblade.modules.admin.vo.ElevatorUpdateHistoryVO;
import org.springblade.modules.admin.service.IElevatorUpdateHistoryService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;

/**
 * 电梯更新历史
 *
 * @author ysq
 * @since 2024-06-03
 */
@RestController
@AllArgsConstructor
@RequestMapping("sida-maintain-admin/elevator-update-history")
@Api(value = "电梯更新历史", tags = "电梯更新历史")
public class ElevatorUpdateHistoryController extends BladeController {

	private final IElevatorUpdateHistoryService elevatorUpdateHistoryService;

	private final IUserService userService;

	/**
	 * 新增 电梯更新历史
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增", notes = "传入elevatorUpdateHistory")
	public R save(@Valid @RequestBody ElevatorUpdateHistory elevatorUpdateHistory) {
		return R.status(elevatorUpdateHistoryService.save(elevatorUpdateHistory));
	}

	/**
	 * 分页 电梯更新历史
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 30)
	@ApiOperation(value = "分页", notes = "传入elevatorUpdateHistory")
	public R<IPage<ElevatorUpdateHistoryVO>> list(ElevatorUpdateHistory elevatorUpdateHistory, Query query) {
		IPage<ElevatorUpdateHistory> pages = elevatorUpdateHistoryService.page(Condition.getPage(query), Condition.getQueryWrapper(elevatorUpdateHistory).orderByDesc("create_time"));
		IPage<ElevatorUpdateHistoryVO> results = Condition.getPage(query);
		List<ElevatorUpdateHistoryVO> rList = new ArrayList<>();
		pages.getRecords().forEach(p -> {
			ElevatorUpdateHistoryVO r = new ElevatorUpdateHistoryVO();
			BeanUtils.copyProperties(p, r);
			if (!Func.isNull(p.getCreateUser())) {
				User exeUser = userService.getById(p.getCreateUser());
				if (!Func.isNull(exeUser)) {
					r.setUpdateUserName(exeUser.getRealName());
				}
			}
			rList.add(r);
		});
		results.setRecords(rList);
		results.setTotal(pages.getTotal());
		results.setSize(pages.getSize());
		results.setCurrent(pages.getCurrent());
		results.setPages(pages.getPages());
		return R.data(results);
	}

	/**
	 * 自定义分页 电梯更新历史
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "分页", notes = "传入elevatorUpdateHistory")
	public R<IPage<ElevatorUpdateHistoryVO>> page(ElevatorUpdateHistoryVO elevatorUpdateHistory, Query query) {
		IPage<ElevatorUpdateHistoryVO> pages = elevatorUpdateHistoryService.selectElevatorUpdateHistoryPage(Condition.getPage(query), elevatorUpdateHistory);
		List<ElevatorUpdateHistoryVO> records = pages.getRecords();
		if (!Func.isEmpty(records)) {
			for (ElevatorUpdateHistoryVO record : records) {
				if (!Func.isNull(record.getCreateUser())) {
					User exeUser = userService.getById(record.getCreateUser());
					if (!Func.isNull(exeUser)) {
						record.setUpdateUserName(exeUser.getRealName());
					}
				}
			}
		}
		pages.setRecords(records);
		return R.data(pages);
	}

}
